namespace TSL.DAL
{
    using Microsoft.Practices.EnterpriseLibrary.Data;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using System.Text;
    using TSL.Model;

    public class BetMenu
    {
        public void AddBetMenu(SYS_BetMenu model)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("TSL_BetMenu_ADD");
            db.AddInParameter(dbCommand, "BetMenu", DbType.AnsiString, model.BetMenu);
            db.AddInParameter(dbCommand, "Caipiao_Id", DbType.AnsiString, model.Caipiao_Id);
            db.AddInParameter(dbCommand, "caipiao_name", DbType.AnsiString, model.caipiao_name);
            db.AddInParameter(dbCommand, "GradeBonus", DbType.AnsiString, model.GradeBonus);
            db.AddInParameter(dbCommand, "state", DbType.AnsiString, model.state);
            db.AddInParameter(dbCommand, "orderid", DbType.AnsiString, model.orderid);
            db.AddInParameter(dbCommand, "totalAmount", DbType.AnsiString, model.totalAmount);
            db.AddInParameter(dbCommand, "Factor", DbType.AnsiString, model.Factor);
            db.AddInParameter(dbCommand, "Context", DbType.AnsiString, model.Context);
            db.AddInParameter(dbCommand, "Exp", DbType.AnsiString, model.Exp);
            db.ExecuteNonQuery(dbCommand);
        }

        public void Delete(string ID)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("TSL_BetMenu_Delete");
            db.AddInParameter(dbCommand, "ID", DbType.AnsiString, ID);
            db.ExecuteNonQuery(dbCommand);
        }

        public void DeleteBetMenu(string ID)
        {
            List<string> strList = new List<string>();
            strList.Add("delete from BetMenu where ID = '" + ID + "'");
            DbHelperSQL.ExecuteSqlTran(strList);
        }

        public bool Exists(string name, string id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from BetMenu");
            strSql.Append(" where caipiao_name=@name and caipiao_id=@caipiao_id");
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@name", SqlDbType.VarChar, 50), new SqlParameter("@caipiao_id", SqlDbType.VarChar, 50) };
            parameters[0].Value = name;
            parameters[1].Value = id;
            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        public List<SYS_BetMenu> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from V_BetMenu ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append("order by caipiao_id,gradebonus,orderid");
            List<SYS_BetMenu> list = new List<SYS_BetMenu>();
            using (IDataReader dataReader = DatabaseFactory.CreateDatabase().ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(this.ReaderBind(dataReader));
                }
            }
            return list;
        }

        public DataSet GetListFromView(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from V_BetMenu");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DatabaseFactory.CreateDatabase().ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        public SYS_BetMenu GetModel(string ID)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("SYS_BetMenu_GetModelByID");
            db.AddInParameter(dbCommand, "ID", DbType.AnsiString, ID);
            SYS_BetMenu model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = this.ReaderBind(dataReader);
                }
            }
            return model;
        }

        public SYS_BetMenu GetModel(string name, string password)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("SYS_BetMenu_GetModel");
            db.AddInParameter(dbCommand, "username", DbType.AnsiString, name);
            db.AddInParameter(dbCommand, "password", DbType.AnsiString, password);
            SYS_BetMenu model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = this.ReaderBind(dataReader);
                }
            }
            return model;
        }

        public DataSet GetMySet(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from V_GetMySet");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append("  order by bigorder,samllorder");
            return DatabaseFactory.CreateDatabase().ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        public DataSet GetPlayInfo(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from V_GetPlayInfo");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append("  order by bigorder,samllorder");
            return DatabaseFactory.CreateDatabase().ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        public SYS_BetMenu ReaderBind(IDataReader dataReader)
        {
            SYS_BetMenu model = new SYS_BetMenu();
            model.id = dataReader["id"].ToString();
            model.SscName = dataReader["SscName"].ToString();
            model.gradebonu = dataReader["gradebonu"].ToString();
            model.GradeBonus = dataReader["gradebonus"].ToString();
            model.BetMenu = dataReader["BetMenu"].ToString();
            model.Caipiao_Id = dataReader["Caipiao_Id"].ToString();
            model.caipiao_name = dataReader["caipiao_name"].ToString();
            model.state = dataReader["state"].ToString();
            model.orderid = dataReader["orderid"].ToString();
            model.Factor = Convert.ToDouble(dataReader["Factor"].ToString());
            model.totalAmount = Convert.ToInt32(dataReader["totalAmount"].ToString());
            model.Context = dataReader["Context"].ToString();
            model.Exp = dataReader["Exp"].ToString();
            return model;
        }

        public void Update(SYS_BetMenu model)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("SYS_BetMenu_Update");
            db.AddInParameter(dbCommand, "ID", DbType.AnsiString, model.id);
            db.AddInParameter(dbCommand, "BetMenu", DbType.AnsiString, model.BetMenu);
            db.AddInParameter(dbCommand, "Caipiao_Id", DbType.AnsiString, model.Caipiao_Id);
            db.AddInParameter(dbCommand, "caipiao_name", DbType.AnsiString, model.caipiao_name);
            db.AddInParameter(dbCommand, "GradeBonus", DbType.AnsiString, model.GradeBonus);
            db.AddInParameter(dbCommand, "state", DbType.AnsiString, model.state);
            db.AddInParameter(dbCommand, "orderid", DbType.AnsiString, model.orderid);
            db.AddInParameter(dbCommand, "totalAmount", DbType.AnsiString, model.totalAmount);
            db.AddInParameter(dbCommand, "Factor", DbType.AnsiString, model.Factor);
            db.AddInParameter(dbCommand, "Context", DbType.AnsiString, model.Context);
            db.AddInParameter(dbCommand, "Exp", DbType.AnsiString, model.Exp);
            db.ExecuteNonQuery(dbCommand);
        }
    }
}

